package org.shj.spark.core

import org.apache.spark.SparkConf
import org.apache.spark.SparkContext

object AccumulatorDemo {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setAppName("AccumulatorDemo").setMaster("local[*]")
    
    val sc = new SparkContext(conf)
    
    val accumulator = sc.longAccumulator("sumAccumulator")
    val rdd = sc.parallelize(1 to 10)
    
    rdd.foreach( i => {
      accumulator.add(i)
      // 下面打印的两个值是一样的
      println(accumulator.sum)
      println(accumulator.value)
    })
    
    sc.stop()
  }
}